// import { useUserStore } from '@/store';
import storage from '@/utils/storage';
import Layout from '@/views/layout/layout-page.vue';
import { createRouter, createWebHistory } from 'vue-router';
import generateRoutes from './generateRoutes';


const routes = [
  { path: '/', redirect: '/main' },
  { 
    path: '/login',
    name: '登录',
    single: true,
    component: () => import('@/views/login/login-page.vue') 
  },
  { 
    path: '/register', 
    name: '注册',
    single: true,
    component: () => import('@/views/login/register-page.vue') 
  },
  {
    path: '/main',
    redirect: '/main/home',
    component: Layout,
    children:[...generateRoutes]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to) => {
  // 权限判断
  if (to.path !== '/login' && to.path !== '/register') { // 不是去往登录页
    // const userStore = useUserStore()
    // const token = userStore.token
    const token = storage.get('token')
    if (!token) { // 没有token 的情况
      return '/login'
    }
  }
})

export default router